function register(){// 新用户注册
	$(`#tip`).html('');
	const code = $.request.queryString["code"];
	const realName = $("#real-name").val();
	const millIid = $.request.queryString["millIid"];
	let millName = '';

	if(!realName){
		alert('必须输入【真实姓名】才能继续操作！');
		return;
	}

	// 禁用按钮，防止多次点击
	document.getElementById("btn").disabled = true;

	const url = getRegisterPostUrl();
	const params = {"code":code,"millIid":millIid,"realName":realName};
	$.ajax({
		url : url,
	  type : "POST",
	  contentType: "application/json;charset=UTF-8",
	  data:JSON.stringify(params),
	  success : function(res) {
			const code = parseInt(res.code);
			if( code !== 1){
				$(`#tip`).html(res.msg);	
				return;
			}

	  	const userInfoString = JSON.stringify(res);
	  	$(`#tip`).html('已提交管理员审核，请耐心等待！');
	  },
	  error : function(e){
	    console.log(e.status);
	    console.log(e.responseText);
	    $(`#tip`).html(e.responseText);
	  },
	  complete: function () {
	    document.getElementById("btn").disabled = false;
	  }
	})
}


$(function(){// 页面启动即执行
	//getCode();
});

function getCode(){// 获取参数 code 的值
	const code = $.request.queryString["code"];
	const state = $.request.queryString["state"];	
	console.log(`code:${code},state:${state}`);
	$(`#code`).html(`code:${code}`);
	$(`#state`).html(`state:${state}`);
	const url = `https://www.xdfznh.club/wxpadbe/dev/getWebAuthorityUserInfo/${code}`;
  $.ajax({
      type : "GET",
      contentType: "application/json;charset=UTF-8",
      url : url,
      success : function(res) {
      	console.log('网页授权请求用户信息响应来的结果是：',res);
      	const userInfoString = JSON.stringify(res);
      	$(`#user-info`).html(userInfoString);
      },
      error : function(e){
        console.log(e.status);
        console.log(e.responseText);
      }
  })
}

$.request = (function () {
	var apiMap = {}; 
	function request(queryStr) { 
			var api = {}; 
			if (apiMap[queryStr]) { return apiMap[queryStr]; } 
			api.queryString = (function () { 
					var urlParams = {}; 
					var e, 
					d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); }, 
					q = queryStr.substring(queryStr.indexOf('?') + 1), 
					r = /([^&=]+)=?([^&]*)/g; 
					while (e = r.exec(q))     urlParams[d(e[1])] = d(e[2]);
					return urlParams; 
			})(); 
			api.getUrl = function () { 
					var url = queryStr.substring(0, queryStr.indexOf('?') + 1); 
					for (var p in api.queryString) { url += p + '=' + api.queryString[p] + "&";        } 
					if (url.lastIndexOf('&') == url.length - 1) { return url.substring(0, url.lastIndexOf('&')); } 
					return url; 
			} 
			apiMap[queryStr] = api; 
			return api; 
	} 
	$.extend(request, request(window.location.href)); 
	return request; 
})();
